import common_url from './common_url.js'
const http = {
	post:function(url,params,flag){
		return new Promise(function(resolve, reject){
			try{
				let data  = params ? params : {};
				if(uni.getStorageSync('titleId') && flag){
					data.zxmbh = uni.getStorageSync('titleId')
				}
				const header = {};
				if(!data.loginout){
					header.token = uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
				}
				console.log(data)
				uni.request({
					url:common_url + '/' + url,
					data:data,
					method:'POST',
					header:header,
					success:(res)=>{
						if(res.data.code == 0){
							resolve(res.data)
						}else if(res.data.code == 401){
							uni.showToast({
								title:res.data.msg,
								icon:"none"
							})
							setTimeout(()=>{
								uni.navigateTo({
									url:'/pages/login/login'
								})
							},1000)
							
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:"none"
							})
						}
					}
				})
			} catch (err){
				console.log(err)
				uni.clearStorageSync()
				uni.reLaunch({
					url:"/pages/login/login"
				})
			}
		})
	},
	upload: function() {
		return new Promise(function(resolve, reject){
			try {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: function(res) {
						let tempFilePath = res.tempFilePaths[0];
						uni.uploadFile({
							url: common_url  + '/' + 'common/imageupload', //仅为示例，非真实的接口地址
							header:{
								"token":uni.getStorageSync('token')
							},
							filePath: tempFilePath,
							name: 'file',
							success: (res1) => {
								let res = JSON.parse(res1.data)
								resolve(res.imagefile)
							}
						});
					}
				});
			}catch(e){
				reject(e)
			}
		}) 
	}
}

export default http;